<script setup lang="ts">
import { computed, reactive, ref, watch } from 'vue';
import { retryArgsTypeRecordOptions, retryStatusTypeOptions } from '@/constants/business';
import { fetchAddRetry, fetchIdempotentIdGenerate } from '@/service/api';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
import { translateOptions } from '@/utils/common';
import OperateDrawer from '@/components/common/operate-drawer.vue';
import { $t } from '@/locales';
import CodeMirror from '@/components/common/code-mirror.vue';
import SelectGroup from '@/components/common/select-group.vue';
import SelectScene from '@/components/common/select-scene.vue';

defineOptions({
  name: 'RetryTaskOperateDrawer'
});

interface Props {
  /** the type of operation */
  operateType: NaiveUI.TableOperateType;
  /** the edit row data */
  rowData?: Api.Retry.Retry | null;
}

const props = defineProps<Props>();

interface Emits {
  (e: 'submitted'): void;
}

const emit = defineEmits<Emits>();

const visible = defineModel<boolean>('visible', {
  default: false
});

const argsList = ref<string[]>([]);
const { formRef, validate, restoreValidation } = useNaiveForm();
const { defaultRequiredRule } = useFormRules();

const title = computed(() => {
  const titles: Record<NaiveUI.TableOperateType, string> = {
    add: $t('page.retry.addRetry'),
    edit: $t('page.retry.editRetry')
  };
  return titles[props.operateType];
});

type Model = CommonType.RecordNullable<
  Pick<
    Api.Retry.Retry,
    | 'groupName'
    | 'sceneName'
    | 'idempotentId'
    | 'bizNo'
    | 'executorName'
    | 'argsStr'
    | 'retryStatus'
    | 'serializerName'
    | 'argsType'
  >
>;

const model: Model = reactive(createDefaultModel());

function createDefaultModel(): Model {
  return {
    groupName: undefined,
    sceneName: undefined,
    idempotentId: '',
    bizNo: '',
    executorName: '',
    argsStr: '',
    retryStatus: 0,
    serializerName: 'fory',
    argsType: 1
  };
}

type RuleKey = Extract<
  keyof Model,
  'groupName' | 'sceneName' | 'idempotentId' | 'bizNo' | 'executorName' | 'retryStatus' | 'serializerName'
>;

const rules: Record<RuleKey, App.Global.FormRule> = {
  groupName: defaultRequiredRule,
  sceneName: defaultRequiredRule,
  idempotentId: defaultRequiredRule,
  bizNo: defaultRequiredRule,
  executorName: defaultRequiredRule,
  serializerName: defaultRequiredRule,
  retryStatus: defaultRequiredRule
};

function handleUpdateModelWhenEdit() {
  argsList.value = [];

  if (props.operateType === 'add') {
    Object.assign(model, createDefaultModel());
    return;
  }

  if (props.operateType === 'edit' && props.rowData) {
    Object.assign(model, props.rowData);
    argsList.value = JSON.parse(props.rowData.argsStr || '[]');
  }
}

function closeDrawer() {
  visible.value = false;
}

async function handleSubmit() {
  await validate();

  if (props.operateType === 'add') {
    const { groupName, sceneName, idempotentId, bizNo, executorName, retryStatus, serializerName, argsStr, argsType } =
      model;
    const { error } = await fetchAddRetry({
      groupName,
      sceneName,
      idempotentId,
      bizNo,
      executorName,
      argsStr: argsType === 1 ? argsStr : JSON.stringify(argsList.value),
      retryStatus,
      serializerName
    });
    if (error) return;
    window.$message?.success($t('common.addSuccess'));
  }

  closeDrawer();
  emit('submitted');
}

watch(visible, () => {
  if (visible.value) {
    handleUpdateModelWhenEdit();
    restoreValidation();
  }
});

async function setIdempotentId() {
  const groupName = model.groupName;
  const sceneName = model.sceneName;
  const serializerName = model.serializerName;
  const executorName = model.executorName;
  const argsStr = model.argsType === 1 ? model.argsStr : JSON.stringify(argsList.value);
  const { data: idempotentId, error } = await fetchIdempotentIdGenerate({
    groupName,
    sceneName,
    executorName,
    argsStr,
    serializerName
  });
  if (!error) {
    model.idempotentId = idempotentId;
  }
}
</script>

<template>
  <OperateDrawer v-model="visible" :title="title" @handle-submit="handleSubmit">
    <NForm ref="formRef" :model="model" :rules="rules">
      <NFormItem :label="$t('page.retryTask.groupName')" path="groupName">
        <SelectGroup v-model:value="model.groupName" :disabled="props.operateType === 'edit'" />
      </NFormItem>
      <NFormItem :label="$t('page.retryTask.sceneName')" path="sceneName">
        <SelectScene
          v-model:value="model.sceneName"
          :group-name="model.groupName as string"
          :disabled="props.operateType === 'edit'"
        />
      </NFormItem>
      <NFormItem :label="$t('page.retryTask.bizNo')" path="bizNo">
        <NInput
          v-model:value="model.bizNo"
          :placeholder="$t('page.retryTask.form.bizNo')"
          :disabled="props.operateType === 'edit'"
        />
      </NFormItem>
      <NFormItem :label="$t('page.retry.executorName')" path="executorName">
        <NInput
          v-model:value="model.executorName"
          :placeholder="$t('page.retry.form.executorName')"
          :disabled="props.operateType === 'edit'"
        />
      </NFormItem>
      <NFormItem :label="$t('page.retryTask.argsType')" path="argsType">
        <NRadioGroup v-model:value="model.argsType" name="argsType">
          <NSpace>
            <NRadio
              v-for="item in retryArgsTypeRecordOptions"
              :key="item.value"
              :value="item.value"
              :label="$t(item.label)"
            />
          </NSpace>
        </NRadioGroup>
      </NFormItem>
      <NFormItem :label="$t('page.retryTask.serializerName')" path="serializerName">
        <NInput
          v-model:value="model.serializerName"
          :placeholder="$t('page.retryTask.form.serializerName')"
          :disabled="props.operateType === 'edit'"
        />
      </NFormItem>
      <NFormItem :label="$t('page.retry.argsStr')" path="argsStr">
        <NDynamicInput v-if="model.argsType === 2" v-model:value="argsList" :on-create="() => ''">
          <template #default="{ index }">
            <NFormItem
              class="w-full"
              ignore-path-change
              :show-label="false"
              :show-feedback="false"
              :path="`argsStr[${index}]`"
            >
              <CodeMirror v-model="argsList[index]" lang="json" :placeholder="$t('page.retry.argsStr')" />
            </NFormItem>
          </template>
        </NDynamicInput>
        <CodeMirror v-else v-model="model.argsStr as any" lang="js" :placeholder="$t('page.retry.argsStr')" />
      </NFormItem>
      <NFormItem :label="$t('page.retryTask.idempotentId')" path="idempotentId">
        <NInputGroup>
          <NInput
            v-model:value="model.idempotentId"
            :placeholder="$t('page.retryTask.form.idempotentId')"
            :disabled="props.operateType === 'edit'"
          />
          <NTooltip trigger="hover">
            <template #trigger>
              <NButton type="default" ghost :disabled="props.operateType === 'edit'" @click="setIdempotentId">
                <icon-clarity:thin-client-solid class="text-icon" />
              </NButton>
            </template>
            {{ $t('page.retry.generateIdempotentId') }}
          </NTooltip>
        </NInputGroup>
      </NFormItem>
      <NFormItem :label="$t('page.retry.retryStatus')" path="retryStatus">
        <NSelect
          v-model:value="model.retryStatus"
          :placeholder="$t('page.retry.form.retryStatus')"
          :options="translateOptions(retryStatusTypeOptions)"
        />
      </NFormItem>
    </NForm>
    <template #footer>
      <NSpace :size="16">
        <NButton @click="closeDrawer">{{ $t('common.cancel') }}</NButton>
        <NButton type="primary" @click="handleSubmit">{{ $t('common.save') }}</NButton>
      </NSpace>
    </template>
  </OperateDrawer>
</template>

<style scoped></style>
